package cn.edu.baiyunu.myapplication.data.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import cn.edu.baiyunu.myapplication.data.model.Category
import kotlinx.coroutines.flow.Flow

/**
 * 分类数据访问对象
 */
@Dao
interface CategoryDao {
    /**
     * 根据类型获取分类列表
     */
    @Query("SELECT * FROM categories WHERE type = :type ORDER BY orderIndex")
    fun getCategoriesByType(type: String): Flow<List<Category>>
    
    /**
     * 获取所有分类
     */
    @Query("SELECT * FROM categories")
    fun getAllCategories(): Flow<List<Category>>
    
    /**
     * 根据ID获取分类
     */
    @Query("SELECT * FROM categories WHERE id = :id")
    suspend fun getCategoryById(id: String): Category?
    
    /**
     * 插入或替换分类
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertCategory(category: Category)
    
    /**
     * 批量插入分类
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertCategories(categories: List<Category>)
    
    /**
     * 更新分类
     */
    @Update
    suspend fun updateCategory(category: Category)
    
    /**
     * 删除分类
     */
    @Delete
    suspend fun deleteCategory(category: Category)
    
    /**
     * 根据ID删除分类
     */
    @Query("DELETE FROM categories WHERE id = :id")
    suspend fun deleteCategoryById(id: String)
    
    /**
     * 获取特定类型的最大排序索引
     */
    @Query("SELECT COALESCE(MAX(orderIndex), 0) FROM categories WHERE type = :type")
    suspend fun getMaxOrderIndexByType(type: String): Int
} 